package cn.telling.sellercentre.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.telling.common.pager.PageVo;
import cn.telling.common.util.TellingConstance;
import cn.telling.product.vo.ShopProductVo;
import cn.telling.productapprove.service.IProductApproveService;
import cn.telling.productapprove.vo.ProductApproveVo;
import cn.telling.sellerCenter.service.ISellerCentreService;
import cn.telling.sellerCenter.vo.ShopProductApproveVo;
import cn.telling.sellerCenter.vo.ShopProductAreaApproveVo;
import cn.telling.sellerCenter.vo.ShopProductAreaVo;
import cn.telling.sellercentre.dao.ISellerCentreDao;
import cn.telling.utils.StringHelperTools;

/**
 * @Description:卖家中心产品业务处理层实现
 * @author liugeng 
 */
@Service("sellerCentreService")
public class SellerCentreServiceImpl implements ISellerCentreService {

	@Autowired
	@Qualifier("SellerCentreDao")
	private ISellerCentreDao sellerCentreDao;
	
	@Autowired
	@Qualifier("ProductApproveService")
	private IProductApproveService productApproveService;
	public IProductApproveService getProductApproveService() {
		return productApproveService;
	}
	public void setProductApproveService(IProductApproveService productApproveService) {
		this.productApproveService = productApproveService;
	}

	/**根据用户Id获得商铺简介的信息*/
	/*@Override
	public SupplyVo GetShopInfo(String userId) {
		return sellerCentreDao.GetShopInfo(userId);
	}*/

	/**根据用户Id修改商铺简介信息*/
	@Override
	public int SetShopIntroduce(String userId, String shopIntroduce) {
		return sellerCentreDao.SetShopIntroduce(userId, shopIntroduce);
	}
	
	/**根据用户Id获得供应商类型    空   外部供应商  01   天音供应商     02 03厂家供应商*/
	@Override
	public String GetSupplyType(String userId) {
		return sellerCentreDao.GetSupplyType(userId);
	}
	
	/**获得卖家中心已上架、已下架、审核中产品列表 */
	@Override
	public List<ShopProductAreaVo> getSaleListByStatus(String userId,String saleType, String status, String conditions) {
		if(!"".equals(saleType) && saleType != null){
			saleType = getSaleType(Integer.parseInt(saleType));
		}
		
		return sellerCentreDao.getSaleListByStatus(userId, saleType, status, conditions);
	}
	/**根据产品列表 进行分类处理 */
	@Override
	public HashMap<String, List<ShopProductAreaVo>> getSaleListMap(List<ShopProductAreaVo> shopProductAreaVoList) {
		
		List<ShopProductAreaVo> newShopProductAreaVoList = new LinkedList<ShopProductAreaVo>();
		HashMap<String, List<ShopProductAreaVo>> shopProductAreaVoListMap = new LinkedHashMap<String, List<ShopProductAreaVo>>();
		
		if(shopProductAreaVoList != null && shopProductAreaVoList.size() > 0){
			for(int i = 0; i < shopProductAreaVoList.size(); i++){
				ShopProductAreaVo shopProductAreaVo = shopProductAreaVoList.get(i);
				if("1".equals(shopProductAreaVo.getIsShare())){
					if(shopProductAreaVoListMap.get(shopProductAreaVo.getShareCode()) != null){
						shopProductAreaVoListMap.get(shopProductAreaVo.getShareCode()).add(shopProductAreaVo);
					}else {
						List<ShopProductAreaVo> list = new ArrayList<ShopProductAreaVo>();
						list.add(shopProductAreaVo);
						
						shopProductAreaVoListMap.put(shopProductAreaVo.getShareCode(), list);
					}
				}else {
					newShopProductAreaVoList.add(shopProductAreaVo);
				}
			}
		}
		
		if(newShopProductAreaVoList != null && newShopProductAreaVoList.size() > 0){
			shopProductAreaVoListMap.put("0", newShopProductAreaVoList);
		}
		return shopProductAreaVoListMap;
	}
	
	/**已上架产品修改*/
	@Override
	public int onSaleModify(ShopProductAreaApproveVo shopProductAreaApproveVo) {
		if(shopProductAreaApproveVo != null){
			List<ShopProductAreaVo> shopProductAreaVoList = shopProductAreaApproveVo.getShopProductAreaVoList();
			if(shopProductAreaVoList != null && shopProductAreaVoList.size() > 0){
				for (int i = 0; i < shopProductAreaVoList.size(); i++) {
					ShopProductAreaVo shopProductAreaVo = shopProductAreaVoList.get(i);
					boolean hasChange = false;
					String remark = "";
					
					/**有价格修改*/
					BigDecimal priceRetailOnline = shopProductAreaVo.getPriceRetailOnline().setScale(2,BigDecimal.ROUND_HALF_EVEN);
					BigDecimal approvePrice = shopProductAreaVo.getApprovePrice().setScale(2, BigDecimal.ROUND_HALF_EVEN);
					if (!priceRetailOnline.equals(approvePrice)) {
						remark = "价格修改为:" + approvePrice.toString();
						hasChange = true;
					}
					
					/**有库存修改*/
					BigDecimal stockNumber = shopProductAreaVo.getStockNumber();
					BigDecimal approveStockNumber = new BigDecimal(shopProductAreaVo.getApproveStockNum());
					if (!stockNumber.equals(approveStockNumber)) {
						if (hasChange) {
							/**有价格修改*/
							remark += ",";
						}
						remark += "库存修改为:"+ approveStockNumber.toString();
						
						hasChange = true;
					}
					/**打补丁的做法  减去现有库存  因为在审核的时候是把原有库存加上的*/
					shopProductAreaVo.setApproveStockNum(Long.parseLong(String.valueOf(approveStockNumber.subtract(stockNumber))));
					
					String saleStatus = shopProductAreaVo.getSaleStatus();
					if("1".equals(saleStatus)){
						remark += "申请下架";
					}else if ("2".equals(saleStatus)) {
						remark += "申请上架";
					}
					
					if (hasChange) {
						shopProductAreaVo.setRemark(remark);
						continue;
					}
					
					shopProductAreaVoList.remove(shopProductAreaVo);
				}
			}
			
			/**没有进行修改*/
			if(shopProductAreaVoList.size() == 0){
				return 0;
			}
			shopProductAreaApproveVo.setShopProductAreaVoList(shopProductAreaVoList);
			
			/**根据列表里大小获得生成的审核Id*/
			String approveId[] = new String[shopProductAreaVoList.size()];
			for(int i = 0; i < shopProductAreaVoList.size(); i++){
				approveId[i] = productApproveService.findApproveId().toString();
				shopProductAreaApproveVo.setId(approveId);
			}
			
			/**根据供货商产品信息提交产品审核申请*/
			sellerCentreDao.insertShopProductApprove(shopProductAreaApproveVo);
			/**更新供应商产品区域销售状态*/
			sellerCentreDao.updateSupplyAreaSaleStatus(shopProductAreaApproveVo);
			 
			/**已上架产品 修改后自动审核  开始    除定向发布的集采产品不自动审核外其余都自动审核*/
			 int productApproveNum = shopProductAreaVoList.size();
			if(shopProductAreaApproveVo.isBlean()){
				/**1：竞价发表2:非定向发布，3：定向发布(到客户)，4：定向发布(到区域) */
				String saleType = StringHelperTools.nvl(shopProductAreaApproveVo.getSaleType());
				String product_del10 = StringHelperTools.nvl(shopProductAreaApproveVo.getProduct_del10());
				if(!((saleType.equals(TellingConstance.SALETYPE3) || saleType.equals(TellingConstance.SALETYPE4)) && "集采".equals(product_del10))){
					int count = 0;
					for (int i = 0; i < approveId.length; i++) {
						ProductApproveVo productApproveVo = new ProductApproveVo();
						productApproveVo.setId(shopProductAreaApproveVo.getId()[i]);
						productApproveVo.setApproveUser("1");// 默认为超级
						productApproveVo.setApproveStatus(TellingConstance.SALESTATUS3);//审核通过
						productApproveVo.setIsSysApprove("1");//系统自动审核
						count += productApproveService.Approve(productApproveVo);
					}
					
					productApproveNum = count;
				}
			}
			/**已上架产品 修改后自动审核  结束*/
			
			return productApproveNum;
		}
		return 0;
	}
	
	/**根据共享编码查询出共享产品列表  供共享使用*/
	@Override
	public Map<String, ShopProductAreaVo> getSupplyAreaByShareCode(String shareCode) {
		Map<String, ShopProductAreaVo> shopProductAreaVoMap = new HashMap<String, ShopProductAreaVo>();
		List<ShopProductAreaVo> shopProductAreaVoList = sellerCentreDao.getSupplyAreaByShareCode(shareCode);
		
		if(shopProductAreaVoList != null && shopProductAreaVoList.size() > 0){
			for (ShopProductAreaVo shopProductAreaVo : shopProductAreaVoList) {
				String supplyAreaId = String.valueOf(shopProductAreaVo.getSupplyAreaId());
				shopProductAreaVoMap.put(supplyAreaId, shopProductAreaVo);
			}
			return shopProductAreaVoMap;
		}
		return null;
	}
	
	/**根据supplyAreaId 查询出供货商产品区域*/
	@Override
	public ShopProductAreaVo getSupplyAreaById(String supplyAreaId) {
		return sellerCentreDao.getSupplyAreaById(supplyAreaId);
	}
	
	/**已上架产品 下架操作*/
	@Override
	public int onSaleOff(ShopProductAreaApproveVo shopProductAreaApproveVo) {
		if(shopProductAreaApproveVo != null){
			List<ShopProductAreaVo> shopProductAreaVoList = shopProductAreaApproveVo.getShopProductAreaVoList();
			if(shopProductAreaVoList != null && shopProductAreaVoList.size() > 0){
				for (int i = 0; i < shopProductAreaVoList.size(); i++) {
					ShopProductAreaVo shopProductAreaVo = shopProductAreaVoList.get(i);
					if ("1".equals(shopProductAreaVo.getSaleStatus())) {
						/**判断是否竞价状态*/
						if (TellingConstance.SALETYPE1.equals(shopProductAreaVo.getSaleType())) {
							sellerCentreDao.UpdateContinuous(shopProductAreaVo);
							shopProductAreaVoList.remove(shopProductAreaVo);
						} else {
							shopProductAreaVo.setRemark("商品下架");
						}
					}else if("2".equals(shopProductAreaVo.getSaleStatus())) {
						shopProductAreaVo.setRemark("商品上架");
						BigDecimal stockNumber = shopProductAreaVo.getStockNumber();
						BigDecimal approveStockNumber = new BigDecimal(shopProductAreaVo.getApproveStockNum());
						shopProductAreaVo.setApproveStockNum(Long.parseLong(String.valueOf(approveStockNumber.subtract(stockNumber))));
					}else {
						shopProductAreaVoList.remove(shopProductAreaVo);
					}
				}
			}
			
			/**没有进行修改*/
			if(shopProductAreaVoList.size() == 0){
				return 0;
			}
			shopProductAreaApproveVo.setShopProductAreaVoList(shopProductAreaVoList);
			
			/**根据列表里大小获得生成的审核Id*/
			String approveId[] = new String[shopProductAreaVoList.size()];
			for(int i = 0; i < shopProductAreaVoList.size(); i++){
				approveId[i] = productApproveService.findApproveId().toString();
				shopProductAreaApproveVo.setId(approveId);
			}
			
			/**根据供货商产品信息提交产品审核申请*/
			sellerCentreDao.insertShopProductApprove(shopProductAreaApproveVo);
			/**更新供应商产品区域销售状态*/
			sellerCentreDao.updateSupplyAreaSaleStatus(shopProductAreaApproveVo);
			
			/**已上架产品 修改后自动审核  开始    除定向发布的集采产品不自动审核外其余都自动审核*/
			 int productApproveNum = shopProductAreaVoList.size();
			if(shopProductAreaApproveVo.isBlean()){
				int count = 0;
				for (int i = 0; i < shopProductAreaVoList.size(); i++) {
					ShopProductAreaVo shopProductAreaVo = shopProductAreaVoList.get(i);
					/**1：竞价发表2:非定向发布，3：定向发布(到客户)，4：定向发布(到区域) */
					String saleType = StringHelperTools.nvl(shopProductAreaVo.getSaleType());
					String product_del10 = StringHelperTools.nvl(shopProductAreaVo.getProduct_del10());
					if(!((saleType.equals(TellingConstance.SALETYPE3) || saleType.equals(TellingConstance.SALETYPE4)) && "集采".equals(product_del10))){
						ProductApproveVo productApproveVo = new ProductApproveVo();
						productApproveVo.setId(shopProductAreaApproveVo.getId()[i]);
						productApproveVo.setApproveUser("1");// 默认为超级
						productApproveVo.setApproveStatus(TellingConstance.SALESTATUS3);//审核通过
						productApproveVo.setIsSysApprove("1");//系统自动审核
						count += productApproveService.Approve(productApproveVo);
						productApproveNum = count;
					}
				}
			}
			/**已上架产品 修改后自动审核  结束*/
			return productApproveNum;
		}
		return 0;
	}
	
	/**同一编码更新同一库存*/
	@Override
	public int updateShareStockNumber(String stockNumber, String shareCode) {
		return sellerCentreDao.updateShareStockNumber(stockNumber, shareCode);
	}
	
	
	
	
	
	
	
	
	
	
	/**根据用户Id、销售类型(定向、非定向)、上下架状态、型号名称   获得供货商产品总数*/
	@Override
	public int GetShopProductCount(String userId, int saleType, String status,String modelName) {
		int count = 0;
		if (saleType == 0) {
			//获取所有销售类型产品总数
			count = sellerCentreDao.GetShopProductCount(userId, null, status,modelName);
		} else {
			//获取指定销售类型产品总数
			count = sellerCentreDao.GetShopProductCount(userId,GetSaleType(saleType), status,modelName);
		}
		return count;
	}
	
	/**根据用户Id、销售类型(定向、非定向)、上下架状态、型号名称  分页获得供货商产品列表（分页）*/
	@Override
	public List<ShopProductVo> GetShopProductList(String userId, int saleType,String status, PageVo pageVo,String modelName) {
		List<ShopProductVo> shopProductVoList = null;
		if (saleType == 0) {
			//获取所有销售类型产品列表（分页）
			shopProductVoList = sellerCentreDao.GetShopProductList(userId, null, status,pageVo,modelName);
		} else {
			//获取指定销售类型产品列表（分页）
			shopProductVoList = sellerCentreDao.GetShopProductList(userId,GetSaleType(saleType), status, pageVo,modelName);
		}
		return shopProductVoList;
	}
	
	/**根据用户Id、销售类型(定向、非定向)、上下架状态   获得所有供货商产品Id列表（不分页）*/
	@Override
	public List<String> GetShopProductListNoPage(String userId, int saleType,String status) {

		List<ShopProductVo> shopProductVoList = null;
		if (saleType == 0) {
			//获取所有销售类型产品列表（不分页）
			shopProductVoList = sellerCentreDao.GetShopProductListNoPage(userId, null, status);
		} else {
			//获取指定销售类型产品列表（不分页）
			shopProductVoList = sellerCentreDao.GetShopProductListNoPage(userId,GetSaleType(saleType), status);
		}
		
		List<String> shopProductVoIdList = new ArrayList<String>();
		if (shopProductVoList != null && shopProductVoList.size() > 0) {
			for(int i = 0; i < shopProductVoList.size(); i++) {
				shopProductVoIdList.add(String.valueOf(shopProductVoList.get(i).getId()));
			}
		}
		
		return shopProductVoIdList;
	}
	
	/**根据用户Id、销售类型(定向、非定向)、型号名称   获得审核中的供货商产品总数*/
	@Override
	public int GetApproveShopProductCount(String userId, int saleType,String modelName) {
		int count = 0;
		if (saleType == 0) {
			//获取审核中的所有销售类型产品总数
			count = sellerCentreDao.GetApproveShopProductCount(userId, null,modelName);
		} else {
			//获取审核中的指定销售类型产品总数
			count = sellerCentreDao.GetApproveShopProductCount(userId,GetSaleType(saleType),modelName);
		}
		
		return count;
	}
	
	/**根据用户Id、销售类型(定向、非定向)、型号名称  分页获得审核中的供货商产品列表（分页）*/
	@Override
	public List<ShopProductVo> GetApproveShopProductList(String userId, int saleType,PageVo pageVo,String modelName) {

		List<ShopProductVo> shopProductVoList = null;
		if (saleType == 0) {
			//获取审核中的所有销售类型产品列表（分页）
			shopProductVoList = sellerCentreDao.GetApproveShopProductList(userId, null,pageVo,modelName);
		} else {
			//获取审核中的指定销售类型产品列表（分页）
			shopProductVoList = sellerCentreDao.GetApproveShopProductList(userId,GetSaleType(saleType), pageVo,modelName);
		}
		
		return shopProductVoList;
	}
	
	/**根据用户Id、销售类型(定向、非定向)  获得所有审核中的供货商产品Id列表（不分页）*/
	@Override
	public List<String> GetApproveShopProductListNoPage(String userId, int saleType) {

		List<ShopProductVo> shopProductVoList = null;
		if (saleType == 0) {
			//获取审核中的所有销售类型产品列表（不分页）
			shopProductVoList = sellerCentreDao.GetApproveShopProductListNoPage(userId, null);
		} else {
			//获取审核中的所有销售类型产品列表（不分页）
			shopProductVoList = sellerCentreDao.GetApproveShopProductListNoPage(userId,GetSaleType(saleType));
		}
		
		List<String> shopProductVoIdList = new ArrayList<String>();
		if (shopProductVoList != null && shopProductVoList.size() > 0) {
			for(int i = 0; i < shopProductVoList.size(); i++) {
				shopProductVoIdList.add(String.valueOf(shopProductVoList.get(i).getId()));
			}
		}
		
		return shopProductVoIdList;
	}
	
	/**根据传入的销售类型(定向、非定向)返回销售模式*/
	private String getSaleType(int saleType){
		String result = "";
		switch(saleType){
			case 1 :
				result = TellingConstance.SALETYPE3 + "','" + TellingConstance.SALETYPE4;
				break;
			case 2 :
				result = TellingConstance.SALETYPE2;
				break;
			case 3 :
				result = TellingConstance.SALETYPE1;
				break;
			default:
				result = "";
				break;
		}
		return result;
	}
	
	/**根据传入的销售类型(定向、非定向)返回销售模式*/
	private String GetSaleType(int type) {
		String saleType = "";
		switch (type) {
		case 1:
			saleType = TellingConstance.SALETYPE3 + "','" + TellingConstance.SALETYPE4;
			break;
		case 2:
			saleType = TellingConstance.SALETYPE2;
			break;
		case 3:
			saleType = TellingConstance.SALETYPE1;
			break;
		default:
			break;
		}

		return saleType;
	}
	
	/**根据供货商产品Id、销售状态(1 审核中、2 审核未通过、3  审核通过)、用户Id、上下架状态  获得该供货商产品的指定状态的发布信息 */
	@Override
	public ShopProductApproveVo GetApproveShopProductDetailList(String shopProductId, String saleStatus, String userId, String status) {
		
		// 获取供应商产品信息
		ShopProductVo shopProdctVo = sellerCentreDao.GetShopProduct(shopProductId, userId);
		List<ShopProductAreaVo> shopProductAreaVoList = null;
		HashMap<String, List<ShopProductAreaVo>> map = new LinkedHashMap<String, List<ShopProductAreaVo>>();
		
		List<ShopProductAreaVo> newShopProductAreaVoList = new ArrayList<ShopProductAreaVo>();
		
		if (shopProdctVo != null) {
			// 获取供应商产品区域信息
			shopProductAreaVoList = sellerCentreDao.GetShopProductAreaList(shopProductId, saleStatus, status);
		}
		
		if(shopProductAreaVoList != null && shopProductAreaVoList.size() > 0){
			for(int i = 0; i < shopProductAreaVoList.size(); i++){
				ShopProductAreaVo shopProductAreaVo = shopProductAreaVoList.get(i);
				if("1".equals(shopProductAreaVo.getIsShare())){
					if(map.get(shopProductAreaVo.getShareCode()) != null){
						map.get(shopProductAreaVo.getShareCode()).add(shopProductAreaVo);
					}else {
						List<ShopProductAreaVo> list = new ArrayList<ShopProductAreaVo>();
						list.add(shopProductAreaVo);
						
						map.put(shopProductAreaVo.getShareCode(), list);
					}
					
				}else {
					newShopProductAreaVoList.add(shopProductAreaVo);
				}
			}
		}
		
		map.put("0", newShopProductAreaVoList);

		ShopProductApproveVo shopProductApproveVo = new ShopProductApproveVo();
		shopProductApproveVo.setShopProductVo(shopProdctVo);
		shopProductApproveVo.setLstShopProductAreaVo(shopProductAreaVoList);
		shopProductApproveVo.setShopProductAreaVoMap(map);
		
		return shopProductApproveVo;
	}
	
	/**根据供应商产品Id、用户Id  获得该供货商产品的所有发布信息        供发布产品校验时使用*/
	@Override
	public ShopProductApproveVo GetApproveShopProductListCheck(String shopProductId,String userId) {
		// 获取供应商产品信息
		ShopProductVo shopProdctVo = sellerCentreDao.GetShopProduct(shopProductId, userId);
		List<ShopProductAreaVo> shopProductAreaVoList = null;
				
		if (shopProdctVo != null) {
			// 获取供应商产品区域信息
			shopProductAreaVoList = sellerCentreDao.GetShopProductAreaList(shopProductId);
		}
				
		ShopProductApproveVo shopProductApproveVo = new ShopProductApproveVo();
		shopProductApproveVo.setShopProductVo(shopProdctVo);
		shopProductApproveVo.setLstShopProductAreaVo(shopProductAreaVoList);
		
		return shopProductApproveVo;
	}
	
	/**根据用户Id、销售类型(定向、非定向)  获得所有供货商产品Id列表（不分页,包括审核中的、已上架的、已下架的）   供剔除产品列表使用*/
	@Override
	public List<String> GetShopProductIdListNoPage(String userId,int saleType,String status) {
		List<ShopProductVo> shopProductVoList = null;
		
		if (saleType == 0) {
			//获取供货商产品Id列表（不分页）
			shopProductVoList = sellerCentreDao.GetShopProductIdListNoPage(userId,null,status);
		} else {
			//获取供货商产品Id列表（不分页）
			shopProductVoList = sellerCentreDao.GetShopProductIdListNoPage(userId,GetSaleType(saleType),status);
		}
		
		List<String> shopProductIdList = new ArrayList<String>();
		if (shopProductVoList != null && shopProductVoList.size() > 0) {
			for(int i = 0; i < shopProductVoList.size(); i++) {
				shopProductIdList.add(String.valueOf(shopProductVoList.get(i).getId()));
			}
		}
		
		return shopProductIdList;
	}
	
	
	/**根据供应商产品信息提交产品审核申请*/
	@Override
	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public int ApproveShopProductChange(ShopProductApproveVo shopProductApproveVo) {
		// 获取需要更新List
		List<ShopProductAreaVo> shopProductAreaVoList = shopProductApproveVo.getLstShopProductAreaVo();
		
		// 没有上下架处理，才判断是否有价格或库存修改
		for (int i = shopProductAreaVoList.size() - 1; i >= 0; i--) {
			boolean hasChange = false;
			// 判断是否有修改
			ShopProductAreaVo shopProductAreaVo = shopProductAreaVoList.get(i);
			// 有下架操作
			if ("1".equals(shopProductAreaVo.getSaleStatus())) {
				if (TellingConstance.SALETYPE1.equals(shopProductAreaVo.getSaleType())) {
					// 如果是竞价产品,则判断当前竞价状态
					sellerCentreDao.UpdateContinuous(shopProductAreaVo);
					shopProductAreaVoList.remove(shopProductAreaVo);
					
				} else {
					shopProductAreaVo.setRemark("商品下架");
				}
				continue;
			}
			
			String remark = "";
			
			// 有价格修改
			BigDecimal price = shopProductAreaVo.getPrice().setScale(2,BigDecimal.ROUND_HALF_EVEN);
			BigDecimal approvePrice = shopProductAreaVo.getApprovePrice().setScale(2, BigDecimal.ROUND_HALF_EVEN);
			if (!price.equals(approvePrice)) {
				remark = "修改价格为" + approvePrice.toString();
				hasChange = true;
			}
			
			// 有库存修改
			if (shopProductAreaVo.getApproveStockNum()!=0) {
				if (hasChange) {
					// 有价格修改
					remark += ",";
				}
				remark += "库存变更"+(shopProductAreaVo.getApproveStockNum() >= 0 ? "+" : "" ) + String.valueOf(shopProductAreaVo.getApproveStockNum());
				hasChange = true;
			}
			
			if (hasChange) {
				shopProductAreaVo.setRemark(remark);
				continue;
			}
			// 没有修改，删除该条
			shopProductAreaVoList.remove(shopProductAreaVo);
		}
		
		// 没有修改
		if (shopProductAreaVoList.size() == 0){
			return 0;
		}
		shopProductApproveVo.setLstShopProductAreaVo(shopProductAreaVoList);
		
		//取得生成的Id
		String approveId[]=new String[shopProductAreaVoList.size()];
		for(int i = 0; i < shopProductAreaVoList.size(); i++){
			approveId[i] = getProductApproveService().findApproveId().toString();
			shopProductApproveVo.setId(approveId);
		}
		
		// 生成修改申请
		sellerCentreDao.ApproveShopProductChange2(shopProductApproveVo);

		// 更新状态
		sellerCentreDao.UpdateShopProductStatus(shopProductApproveVo);
		 
		 int a=0;
		//发布产品 自动审核（王震追加2014-2-20） start
		if( shopProductApproveVo.isBlean()){

			//1：竞价发表2:非定向发布，3：定向发布(到客户)，4：定向发布(到区域)
			//定向发布的集采不自动审核
			if((shopProductApproveVo.getSaleType().equals(TellingConstance.SALETYPE3) || shopProductApproveVo.getSaleType().equals(TellingConstance.SALETYPE4))){
        	//以外的场合自动审核
			}else{
        	  
	      		for(int j=0;j<approveId.length;j++){
	    	        //对未勾选的产品审核
	    			ProductApproveVo submitPAVo = new ProductApproveVo();
	    			submitPAVo.setId(shopProductApproveVo.getId()[j]);
	    			submitPAVo.setApproveUser("1");// 默认为超级
	    			submitPAVo.setApproveStatus(TellingConstance.SALESTATUS3);
	    			submitPAVo.setIsSysApprove("1");//系统自动审核
	    			getProductApproveService().Approve(submitPAVo);
	    		}
	      		
	      		a = 1;
			}
		}
		//发布产品 自动审核（王震追加2014-2-20） end
		
		return a;
	}
	
	/**根据供应商产品信息提交上架申请*/
	@Override
	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public int ApproveShopProductOnShelves(ShopProductApproveVo shopProductApproveVo) {
		return sellerCentreDao.ApproveShopProductChange(shopProductApproveVo);
	}
	
	/**根据供应商产品信息提交上架申请 */
	@Override
	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public int ApproveShopProductOnShelves2(ShopProductApproveVo shopProductApproveVo) {
		return sellerCentreDao.ApproveShopProductChange2(shopProductApproveVo);
	}
	
	/**根据供应商产品信息提交上架申请 */
	@Override
	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public int ApproveShopProductReOnShelve(ShopProductApproveVo shopProductApproveVo) {
		
		// 获取需要更新List
		List<ShopProductAreaVo> shopProductAreaVoList = shopProductApproveVo.getLstShopProductAreaVo();
		for (int i = shopProductAreaVoList.size() - 1; i >= 0; i--) {
			boolean hasChange = false;
			// 判断是否有修改
			ShopProductAreaVo shopProductAreaVo = shopProductAreaVoList.get(i);
			// 有上架操作
			if ("1".equals(shopProductAreaVo.getSaleStatus())) {
				if (!TellingConstance.SALETYPE1.equals(shopProductAreaVo.getSaleType())) {
					shopProductAreaVo.setSaleStatus("2");
					shopProductAreaVo.setRemark("商品上架");
				}
				continue;
			}

			String remark = "";
			
			// 有价格修改
			BigDecimal price = shopProductAreaVo.getPrice().setScale(2,BigDecimal.ROUND_HALF_EVEN);
			BigDecimal approvePrice = shopProductAreaVo.getApprovePrice().setScale(2, BigDecimal.ROUND_HALF_EVEN);
			if (!price.equals(approvePrice)) {
				remark = "修改价格为" + approvePrice.toString();
				hasChange = true;
			}
			
			// 有库存修改
			if (shopProductAreaVo.getApproveStockNum()!=0) {
				if (hasChange) {
					// 有价格修改
					remark += ",";
				}
				remark += "库存变更"+(shopProductAreaVo.getApproveStockNum() >= 0 ? "+" : "" ) + String.valueOf(shopProductAreaVo.getApproveStockNum());
				hasChange = true;
			}
			
			if (hasChange) {
				shopProductAreaVo.setRemark(remark);
				continue;
			}
			
			// 没有修改，删除该条
			shopProductAreaVoList.remove(shopProductAreaVo);
		}
		
		// 没有修改
		if (shopProductAreaVoList.size() == 0) {
			return 0;
		}
		
		shopProductApproveVo.setLstShopProductAreaVo(shopProductAreaVoList);
		
		// 生成修改申请
		int i = 0;
		if (!TellingConstance.SALETYPE1.equals(shopProductAreaVoList.get(0).getSaleType())) {
			
			//取得生成的Id
			String approveId[]=new String[shopProductAreaVoList.size()];
			for(int j=0;j<shopProductAreaVoList.size();j++){
				approveId[j]=getProductApproveService().findApproveId().toString();
				shopProductApproveVo.setId(approveId);
			}
			
			i = sellerCentreDao.ApproveShopProductChange2(shopProductApproveVo);
			i = sellerCentreDao.UpdateShopProductStatus(shopProductApproveVo);
			
			//发布产品 自动审核（王震追加2014-2-20） start
			//1：竞价发表2:非定向发布，3：定向发布(到客户)，4：定向发布(到区域)
			//定向发布的集采不自动审核
			
			if( shopProductApproveVo.isBlean()){
	        if( (shopProductApproveVo.getSaleType().equals(TellingConstance.SALETYPE3) || shopProductApproveVo.getSaleType().equals(TellingConstance.SALETYPE4))){

	          }else{
				// 对勾选的产品审核
				for (int k = 0; k < approveId.length; k++) {
					ProductApproveVo submitPAVo = new ProductApproveVo();
					submitPAVo.setId(approveId[k]);
					submitPAVo.setApproveUser("1");// 默认为超级
					submitPAVo.setApproveStatus(TellingConstance.SALESTATUS3);
					submitPAVo.setIsSysApprove("1");//系统自动审核
					i = getProductApproveService().Approve(submitPAVo);
				}
			}
			}
	        
	      //发布产品 自动审核（王震追加2014-2-20） end
		} else {
			i = sellerCentreDao.UpdateShopBidProductStatus(shopProductApproveVo);
		}
		
		// 更新状态
		return i;
	}
	
	/**根据用户Id、销售类型(定向、非定向)、型号名称、供货商产品Id  获得审核中的产品列表数量*/
	@Override
	public int GetApproveShopProductCountByProID(String userId, int saleType, String modelName, String shopProductId) {
		int count = 0;
		if (saleType == 0) {
			// 获取审核中的所有销售类型产品数量
			count = sellerCentreDao.GetApproveShopProductCountByProId(userId, null,modelName,shopProductId);
		} else {
			// 获取审核中的指定销售类型产品数量
			count = sellerCentreDao.GetApproveShopProductCountByProId(userId,GetSaleType(saleType),modelName,shopProductId);
					
		}
		return count;
	}
	
	/**根据用户Id、销售类型(定向、非定向)、上下架状态、型号名称、供货商产品Id 获得所有供货商产品列表数量*/
	@Override
	public int GetShopProductCountByProID(String userId, int saleType, String status, String modelName, String shopProductId) {
		int count = 0;
		if (saleType == 0) {
			// 获取非审核中的所有销售类型产品数量
			count = sellerCentreDao.GetShopProductCountByProId(userId, null, status,modelName,shopProductId);
		} else {
			// 获取非审核中的指定销售类型产品数量
			count = sellerCentreDao.GetShopProductCountByProId(userId, GetSaleType(saleType), status,modelName,shopProductId);
		}
		return count;
	}
	
}